package com.zjzywl.smoke.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zjzywl.smoke.config.AppConfig;
import com.zjzywl.smoke.entity.dto.BuildingWorkOrderList;
import com.zjzywl.smoke.util.BuildingWorkorderLists;
import com.zjzywl.smoke.util.RectificationRemarks;
import com.zjzywl.smoke.util.WebResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/*
 * 工单管理
 */
@Api(value = "楼宇巡检工单管理", description = "巡检工单接口，提供工单的创建、转发、改、查")
@RestController
@RequestMapping(value = "/buildingWorkOrder")
public class BuildingWorkOrderController extends BaseController {
    @Autowired
    private AppConfig appConfig;

    @ApiOperation("小程序获取工单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buildingPeopleId", value = "登陆小程序的用户id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "disposition", value = "处理情况0全部 1未处理 2处理", paramType = "query", dataType = "int") })

    @GetMapping(value = "/getWorkOrder")
    public WebResult<List<BuildingWorkOrderList>> getWorkOrder(
            @RequestParam("buildingPeopleId") Integer buildingPeopleId,
            @RequestParam(value = "disposition", required = false) Integer disposition) {
        try {
            ResponseEntity<Object> history = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/buildingWorkOrder/getWorkOrder?buildingPeopleId="
                            + buildingPeopleId + "&disposition=" + disposition, Object.class);
            return WebResult.OK(history);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.toString());
        }
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "onlyValue", value = "巡检唯一值", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "buildingWorkOrdersCount", value = "工单转交次数", required = true, paramType = "query", dataType = "Int") })
    @GetMapping(value = "/getRectificationRemarks")
    public WebResult<RectificationRemarks> findByOnlyValue(@RequestParam("onlyValue") Long onlyValue,
            @RequestParam(value = "buildingWorkOrdersCount", required = false) int buildingWorkOrdersCount) {
        try {
            ResponseEntity<Object> history = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/buildingWorkOrder/getRectificationRemarks?onlyValue="
                            + onlyValue + "&buildingWorkOrdersCount=" + buildingWorkOrdersCount, Object.class);
            return WebResult.OK(history);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.toString());
        }
    }

    @ApiOperation("小程序存储整改，和备注信息")

    @ApiImplicitParams({
            @ApiImplicitParam(name = "onlyValue", value = "巡检唯一值", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "rectificationRemarks", value = "整改备注", paramType = "boby", dataType = "RectificationRemarks"),
            @ApiImplicitParam(name = "buildingPeopleId", value = "整改备注", paramType = "boby", dataType = "int"),
            @ApiImplicitParam(name = "buildingWorkOrdersCount", value = "工单转交次数", required = true, paramType = "query", dataType = "Int") })

    @PostMapping(value = "/saveRectificationRemarks")
    public WebResult<RectificationRemarks> saveRectificationRemarks(@RequestParam("onlyValue") Long onlyValue,
            @RequestParam("buildingPeopleId") int buildingPeopleId,
            @RequestParam("buildingWorkOrdersCount") int buildingWorkOrdersCount,
            @RequestBody RectificationRemarks rectificationRemarks) {
        try {
            ResponseEntity<Object> history = appConfig.restTemplate()
                    .postForEntity(appConfig.getGetDataIp() + "/buildingWorkOrder/saveRectificationRemarks?onlyValue="
                            + onlyValue + "&buildingPeopleId=" + buildingPeopleId + "&buildingWorkOrdersCount="
                            + buildingWorkOrdersCount, rectificationRemarks, Object.class);
            return WebResult.OK(history);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.toString());
        }
    }

    @ApiOperation("工单转发")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "onlyValue", value = "巡检唯一值", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "buildingPeopleId", value = "登陆小程序的用户id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "accepter", value = "受理人id[按照逗号分割]", required = true, paramType = "query", dataType = "String") ,
            @ApiImplicitParam(name = "remark", value = "备注", required = true, paramType = "query", dataType = "String") })
    @PostMapping(value = "/workOrderForwarding")
    public WebResult workOrderForwarding(@RequestParam("onlyValue") Long onlyValue,
            @RequestParam(value = "buildingPeopleId") int buildingPeopleId, @RequestParam("accepter") String accepter,@RequestParam("remark") String remark) {
        try {
            ResponseEntity<Object> history = appConfig.restTemplate()
                    .postForEntity(
                            appConfig.getGetDataIp() + "/buildingWorkOrder/workOrderForwarding?onlyValue=" + onlyValue
                                    + "&buildingPeopleId=" + buildingPeopleId + "&accepter=" + accepter+"&remark="+remark,
                            null, Object.class);
            return WebResult.OK(history);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.toString());
        }
    }

    @ApiOperation("获取整改过程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "onlyValue", value = "巡检唯一值", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "buildingWorkOrdersCount", value = "工单转交次数", required = true, paramType = "query", dataType = "Int") })
    @GetMapping(value = "/getRectificationProcess")
    public WebResult<List<BuildingWorkorderLists>> getRectificationProcess(@RequestParam("onlyValue") Long onlyValue,
            @RequestParam("buildingWorkOrdersCount") int buildingWorkOrdersCount) {
        try {
            ResponseEntity<Object> history = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/buildingWorkOrder/getRectificationProcess?onlyValue="
                            + onlyValue + "&buildingWorkOrdersCount=" + buildingWorkOrdersCount, Object.class);
            return WebResult.OK(history);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.toString());
        }
    }

    @ApiOperation("拒绝接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "onlyValue", value = "巡检唯一值", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "buildingPeopleId", value = "登陆小程序的用户id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "buildingWorkOrdersCount", value = "工单转交次数", required = true, paramType = "query", dataType = "Int") ,
            @ApiImplicitParam(name = "remark", value = "备注", required = true, paramType = "query", dataType = "String")})
    @GetMapping(value = "/refuse")
    public WebResult refuse(@RequestParam("onlyValue") Long onlyValue,
            @RequestParam("buildingPeopleId") int buildingPeopleId,
            @RequestParam("buildingWorkOrdersCount") int buildingWorkOrdersCount,@RequestParam("remark")String remark) {
        try {
            ResponseEntity<Object> history = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/buildingWorkOrder/refuse?onlyValue=" + onlyValue
                            + "&buildingPeopleId=" + buildingPeopleId + "&buildingWorkOrdersCount="
                            + buildingWorkOrdersCount+"&remark="+remark, Object.class);
            return WebResult.OK(history);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.toString());
        }
    }

    @ApiOperation("结束接口异常结束")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "onlyValue", value = "巡检唯一值", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "buildingPeopleId", value = "登陆小程序的用户id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "buildingWorkOrdersCount", value = "工单转交次数", required = true, paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "remark", value = "备注", required = true, paramType = "query", dataType = "String") })
    @GetMapping(value = "/end")
    public WebResult end(@RequestParam("onlyValue") Long onlyValue,
            @RequestParam("buildingPeopleId") int buildingPeopleId,
            @RequestParam("buildingWorkOrdersCount") int buildingWorkOrdersCount,@RequestParam("remark")String remark) {
        try {
            ResponseEntity<Object> history = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/buildingWorkOrder/end?onlyValue=" + onlyValue
                            + "&buildingPeopleId=" + buildingPeopleId + "&buildingWorkOrdersCount="
                            + buildingWorkOrdersCount+"&remark="+remark, Object.class);
            return WebResult.OK(history);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.toString());
        }
    }
    @ApiOperation("结束接口正常结束")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "onlyValue", value = "巡检唯一值", required = true, paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "buildingPeopleId", value = "登陆小程序的用户id", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "buildingWorkOrdersCount", value = "工单转交次数", required = true, paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "remark", value = "备注", required = true, paramType = "query", dataType = "String") })
    @GetMapping(value = "/normalEnd")
    public WebResult normalEnd(@RequestParam("onlyValue") Long onlyValue,
            @RequestParam("buildingPeopleId") int buildingPeopleId,
            @RequestParam("buildingWorkOrdersCount") int buildingWorkOrdersCount,@RequestParam("remark")String remark) {
        try {
            ResponseEntity<Object> history = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/buildingWorkOrder/normalEnd?onlyValue=" + onlyValue
                            + "&buildingPeopleId=" + buildingPeopleId + "&buildingWorkOrdersCount="
                            + buildingWorkOrdersCount+"&remark="+remark, Object.class);
            return WebResult.OK(history);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.toString());
        }
    }
    @ApiOperation("跳转详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "onlyValue", value = "巡检唯一值", required = true, paramType = "query", dataType = "Long"),@ApiImplicitParam(name = "onlyValue", value = "巡检唯一值", required = true, paramType = "query", dataType = "Long") })
    @GetMapping(value = "/skip")
    public WebResult<BuildingWorkOrderList> skip(@RequestParam("onlyValue") Long onlyValue,@RequestParam("userId") int userId) {
        try {
            ResponseEntity<Object> history = appConfig.restTemplate()
                    .getForEntity(appConfig.getGetDataIp() + "/buildingWorkOrder/skip?onlyValue=" + onlyValue+"&userId="+userId, Object.class);
            return WebResult.OK(history);
        } catch (Exception e) {
            e.printStackTrace();
            return WebResult.FAIL(e.toString());
        }
    }
}
